import type { FormDataType } from '@/types/user'
import { getToken, getCoUserInfo, removeCoToken, setCoToken, setCoUserInfo, removeCoUserInfo } from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref } from 'vue'
// 创建user
export const useUserStore = defineStore('hm-user', () => {
  // token
  const token = ref<string>(getToken() || '')
  // 设置token ----------------------------------------------------------
  const setToken = (val: string) => {
    token.value = val
    setCoToken(val)
  }
  // 删除token
  const removeToken = () => {
    removeCoToken()
    token.value = ''
  }

  // 用户名密码 ----------------------------------------------------------
  const userInfo = ref<FormDataType>(getCoUserInfo() ||{
    password: '',
    username: ''
  })
  // 设置用户名密码
  const setUserInfo = (val: FormDataType) => {
    userInfo.value = val
    setCoUserInfo(val)
  }
  // 删除用户名密码
  const removeUserInfo = () => {
    removeCoUserInfo()
    userInfo.value = {
      password: '',
      username: ''
    }
  }
  return {
    token,
    setToken,
    removeToken,
    userInfo,
    setUserInfo,
    removeUserInfo
  }
})
